package com.wedo.mall.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import com.e.library.dialog.EDialogFragment;
import com.wedo.mall.R;
import com.wedo.mall.entity.Goods;
import com.wedo.mall.widget.AttrWidget;
import butterknife.BindView;

/**
 * 选择商品属性对话框
 * Created by e on 2017/7/10.
 */
public class AttrDialog extends EDialogFragment {

    public AttrWidget widget = new AttrWidget();
    @BindView(R.id.fl_root) FrameLayout fl_root;

    public static AttrDialog with(Goods goods){
        Bundle bundle = new Bundle();
        bundle.putParcelable("goods",goods);
        AttrDialog dialog = new AttrDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    protected int layout() {
        return R.layout.fragment_attr;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Goods goods = getArguments().getParcelable("goods");
        widget.init(getContext(),goods);
        widget.attach(fl_root);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = new Dialog(getActivity(), R.style.dialog_bottom);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(layout());
        dialog.getWindow().setWindowAnimations(com.e.library.R.style.SingleDialogWindowAnim);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.setCanceledOnTouchOutside(true);

        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);

        return dialog;
    }
}
